var ip33Url = "http://api.ip33.com/ip/search",
    netBasePath = "http://sso.stp.com",
    wsBasePath = "ws://sso.stp.com"


/**
 * token存储参考：【https://zhuanlan.zhihu.com/p/469646125】
 */
function addToken2Headers(headers){
    let token = localStorage.getItem('token');
    if (token) {
        headers = headers || {};
        // headers.Accept= "application/json; charset=utf-8"
        headers.token = ''+token
    }else {
        console.log('本地token不存在')
    }
    return headers;
}
var getToken = function(){
    let k = 'token',
        v = ''+localStorage.getItem('token')
    console.log(k,':',v)
    return {
        k: v
    }
}
/*function myAjax(url, options) {
    // 创建 XMLHttpRequest 对象
    const xhr = new XMLHttpRequest();

    if (options) {
        if (!options.method){
            options.method = 'POST'
        }
        //未定义则不携带
        if(options.headers === undefined){
        //定义且赋值
        }else if(options.headers != null){
        //定义且未赋值
        }else{
            options.headers = addToken2Headers()
            xhr.setRequestHeader('token',options.headers)
        }
        if(!options.async){
            options.async = false
        }
        if(!options.contentType){
            options.contentType = 'application/json'
        }
        if(!options.data){
            options.data = ''
        }
    }

    // 设置请求方法和请求头
    xhr.open(options.method, url.concat('http')?url:baseUrl+url, options.async);
    xhr.setRequestHeader('Content-Type', options.contentType);

    // 处理响应
    xhr.onload = function () {
        if (xhr.status >= 200 && xhr.status < 300) {
            // 成功处理响应数据
            try {
                const responseData = JSON.parse(xhr.responseText);
                options.success(responseData);
            }catch (errorMsg){
                console.log("响应数据解析异常："+errorMsg)
                alert(errorMsg);
            }finally {
                console.log("响应数据解析异常")
            }
        } else {
            // 错误处理
            options.error(xhr.statusText);
        }
    };

    // 发送请求
    xhr.send(options.data);
}*/

//3秒后返回登录页
function toLoginPage(){
    setTimeout(function (){
        location.href = "login.html";
    },3000)
}
//请求成功但响应状态异常
function requestOkButCodeError(json){
    if (json.msg){
        alert("请求成功，但返回状态码异常！"+json.msg)
    }else {
        alert("请求成功，但返回状态码异常！"+json)
    }
    // toLoginPage();
}
//默认请求成功
function success(r){
    if (r.code==200){
        console.log("请求成功："+r)
    }else {
        requestOkButCodeError(r);
    }
}
//默认请求失败
function error(xhr){
    if (xhr.code){
        alert("请求失败！"+xhr.code);
    }else {
        alert("请求失败！"+xhr.responseText);
    }
}
//请求失败且返回登录页
function error2Login(xhr){
    alert("请求失败，即将返回登录页……"+xhr.code);
    // toLoginPage();
}
//获取ip成功
function searchIpSuccess(r){
    if (r){
        $("#clientIp").val(r.ip);
        $("#clientArea").val(r.area);
    }
}


function name2ClassObj(name){
    return $('div.'+name);
}

//检查input非空
function checkNotNull(name,msg) {
    //移除信息
    $('div.'+name).find(".msg").remove();
    if ($('#' + name).val()) {
        $('#' + name).css("border", "");
        return true;
    } else {
        $('#' + name).css("border", "1px solid red");
        $('div.'+name).append("<span class='msg onError'>" + msg + "</span>");
        return false;
    }
}
//组件失去焦点事件开始校验
function blurCheck(name,msg){
    $('#' + name).blur(function (){
        checkNotNull(name,msg);
    });
}
//多个标签一起校验(烂尾)
function checkValidator(options){
    if(!options || !options.rules) return;
    let rules,message;
    rules = options.rules;
    for (let name in rules){
        console.log('多个标签一起校验:',name);
    }
}
//多个标签一起校验(发生事件后再校验)
function multipleCheckBlur(obj){
    //循环若有一次是false，则结果为false
    for (let key in obj){
        blurCheck(key,obj[key]);
    }
}
//多个标签一起校验(直接校验且有返回值)
function multipleCheckResult(obj){
    let res = true;
    //循环若有一次是false，则结果为false
    for (let key in obj){
        res = res && checkNotNull(key,obj[key]);
    }
    return res;
}
//直接校验无返回值
function multipleCheck(obj){
    for (let key in obj){
        checkNotNull(key,obj[key]);
    }
}
//初始化多参校验
let initMultipleCheck = function (p,rules){
    switch (p){
        case 1://发生事件后再校验
            multipleCheckBlur(rules);
            break;
        case 2://直接校验且有返回值
            return multipleCheckResult(rules);
        default://默认直接校验无返回值
            multipleCheck(rules);
            break;
    }
}